Skip to content

feat: add sonatype script (CM-1309) - #4299

Merged
ulemons merged 2 commits into
mainfrom
feat/add-sonatype-script
Jul 3, 2026
Merged

feat: add sonatype script (CM-1309)#4299
ulemons merged 2 commits into
mainfrom
feat/add-sonatype-script

Conversation

@ulemons

@ulemons ulemons commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds the one-shot ingestion of the Sonatype popularity signal into the osspckgs packages table.

Sonatype could not deliver raw monthly Maven download counts, so instead they provide a popularity score derived from their SCA telemetry — normalized 0–100 per ecosystem, tiered P0–P3. First delivery is Maven P0 only (top ~500 components). This PR loads that CSV into the sonatype_* columns.

This is step 2 of 3:

  • Step 1 (separate PR) — schema: sonatype_* columns on packages.
  • This PR — one-shot CSV ingestion script + the DAL upsert it uses.
  • Step 3 (follow-up) — feed sonatype_popularity_score into rank_packages() as a 4th signal.

Because Sonatype's list is "what industry actually uses", some rows don't exist in packages yet (deps.dev hasn't backfilled them). The script inserts them if missing so the signal isn't dropped — the row is then promoted to critical by the ranking pass and backfilled by the daily Maven enrichment worker.

Changes

  • New script services/apps/packages_worker/src/maven/scripts/importSonatypePopularityFromCsv.ts (follows the existing importMaintainersFromCsv.ts pattern, reuses parseCsv):
    • Args: <input.csv>, --snapshot-date=YYYY-MM-DD (derived from the filename if omitted), --dry-run.
    • Validates the CSV header exactly; skips non-Maven rows and malformed rows with a logged reason (no silent drops).
    • Cross-checks component_key against namespace:name.
    • Runs the whole import in a single transaction (dataset is small → all-or-nothing).
    • Reports inserted / updated / skipped, and lists the newly-inserted packages (the small set deps.dev hasn't backfilled) for an audit trail.
  • New DAL function upsertSonatypePopularity() in data-access-layer/src/osspckgs/packages.ts — a dedicated upsert (not upsertPackage) because:
    • it must preserve ingestion_source on conflict (existing rows keep how they were ingested); upsertPackage overwrites it;
    • it writes only the identity + sonatype_* columns on insert (ingestion_source='sonatype'), leaving the rest for later backfill.
    • Conflict target is the composite key (ecosystem, COALESCE(namespace,''), name), not purl — so updating the ~471 existing rows never depends on the stored purl format. New rows get a version-stripped purl pkg:maven/{groupId}/{artifactId}, matching how Maven purls are stored (verified against the DB).
    • Insert-vs-update is detected with a pre-INSERT existing CTE (robust; avoids the xmax=0 internals hack).
  • New type IDbSonatypePopularityUpsert in data-access-layer/src/osspckgs/types.ts.

Type of change

  • Bug fix
  • New feature
  • Refactor / cleanup
  • Performance improvement
  • Chore / dependency update
  • Documentation

JIRA ticket

CM-1309


Note

Medium Risk
Writes directly to the core packages table and can insert new stub rows, but scope is a small idempotent one-shot import with validation, dry-run, and transactional all-or-nothing semantics.

Overview
Adds one-shot ingestion of Sonatype’s Maven popularity CSV (score, rank, tier) into osspckgs packages sonatype_* columns, as step 2 before wiring the signal into rank_packages().

A new importSonatypePopularityFromCsv script (same family as maintainer CSV import) parses and validates the Sonatype header/rows, supports --dry-run and --snapshot-date, runs the load in a single transaction, and logs inserted vs updated rows plus any new package stubs created from the feed.

The DAL adds upsertSonatypePopularity and IDbSonatypePopularityUpsert: upserts on (ecosystem, namespace, name) (not purl), updates only sonatype_* on conflict (ingestion_source unchanged), and inserts minimal rows (ingestion_source='sonatype', version-stripped Maven purl) when a component isn’t in packages yet. package.json gains import:sonatype-popularity npm scripts to run it locally or in prod.

Reviewed by Cursor Bugbot for commit bee6a29. Bugbot is set up for automated code reviews on this repo. Configure here.

@ulemons ulemons self-assigned this Jul 3, 2026
Copilot AI review requested due to automatic review settings July 3, 2026 09:43
@ulemons ulemons added the Feature Created by Linear-GitHub Sync label Jul 3, 2026
Signed-off-by: Umberto Sgueglia <usgueglia@contractor.linuxfoundation.org>

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit d2d4e7a. Configure here.

Signed-off-by: Umberto Sgueglia <usgueglia@contractor.linuxfoundation.org>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements step 2 of 3 of the CM-1309 pipeline: a one-shot ingestion of Sonatype's Maven "popularity score" signal into the osspckgs packages table. Since Sonatype cannot deliver raw Maven download counts, they provide a normalized 0–100 popularity score (tiered P0–P3); this PR loads the first delivery (Maven P0, ~500 components) from a CSV. Because Sonatype's list reflects real-world usage, some components may not yet exist in packages, so the import inserts them (with ingestion_source='sonatype') for later backfill, while preserving ingestion_source on existing rows.

Changes:

  • New one-shot script importSonatypePopularityFromCsv.ts that validates the CSV header, parses/validates rows (with --dry-run and --snapshot-date support), and applies all rows in a single transaction with insert/update/skip reporting.
  • New DAL function upsertSonatypePopularity() keyed on the composite identity (ecosystem, COALESCE(namespace,''), name) that updates only sonatype_* columns on conflict (preserving ingestion_source) and detects insert-vs-update via a pre-INSERT existing CTE.
  • New IDbSonatypePopularityUpsert type describing the upsert payload.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
services/apps/packages_worker/src/maven/scripts/importSonatypePopularityFromCsv.ts New CSV import script: header validation, per-row parse/validate, single-transaction upsert, audit reporting of newly inserted packages.
services/libs/data-access-layer/src/osspckgs/packages.ts Adds upsertSonatypePopularity() — composite-key upsert that preserves ingestion_source and returns insert-vs-update.
services/libs/data-access-layer/src/osspckgs/types.ts Adds IDbSonatypePopularityUpsert type for the new upsert payload.

Notes verified during review: the composite conflict target matches the existing unique index (initial_schema.sql:82); all NOT NULL packages columns omitted from the new INSERT have defaults (created_at gained DEFAULT NOW() in V1780600000), so inserts won't fail; and the pre-INSERT existing CTE reliably detects insert-vs-update since data-modifying CTEs share one snapshot.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copilot AI review requested due to automatic review settings July 3, 2026 09:50

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

@ulemons
ulemons merged commit cf92690 into main Jul 3, 2026
17 checks passed
@ulemons
ulemons deleted the feat/add-sonatype-script branch July 3, 2026 10:42
skwowet pushed a commit that referenced this pull request Jul 3, 2026
Signed-off-by: Umberto Sgueglia <usgueglia@contractor.linuxfoundation.org>
skwowet pushed a commit that referenced this pull request Jul 3, 2026
Signed-off-by: Umberto Sgueglia <usgueglia@contractor.linuxfoundation.org>
Signed-off-by: Yeganathan S <63534555+skwowet@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Feature Created by Linear-GitHub Sync

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants